from functools import partial


class Foo(object):

    def __init__(self):
        self.request = "request"
        self.session = "session"


foo = Foo()


def func(args):
    return getattr(foo, args)


re_func = partial(func, 'request')  # 帮助源函数传递值
se_func = partial(func, 'session')

print(re_func())
print(se_func())


class LocalProxy(object):

    def __init__(self,local):
        self._local = local

    def _get_current_object(self):
        return self._local()

    def __getitem__(self, item):
        return getattr(self._get_current_object(),item)

request = LocalProxy(re_func)
ret = request._get_current_object().method
print(ret)

ret = request['method']
print(ret)

session = LocalProxy(se_func)
print(session._get_current_object())